// Problem: F. Gifts from Grandfather Ahmed
// Contest: Codeforces - Codeforces Round 860 (Div. 2)
// URL: https://codeforces.com/contest/1798/problem/F
// Memory Limit: 256 MB
// Time Limit: 1000 ms
#include<bits/stdc++.h>
using namespace std;
#define MP make_pair
#define debug(x) cerr<<#x<<" = "<<x<<" ";
#define debug_(x) cerr<<#x<<" = "<<x<<"\n";
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
constexpr int P = 998244353;
int norm(int x) {
if (x < 0) {
x += P;
}
if (x >= P) {
x -= P;
}
return x;
}
template<class T>
T power(T a, ll b) {
T res = 1;
for (; b; b /= 2, a *= a) {
if (b % 2) {
res *= a;
}
}
return res;
}
struct Z {
int x;
Z(int x = 0) : x(norm(x)) {}
int val() const {
return x;
}
Z operator-() const {
return Z(norm(P - x));
}
Z inv() const {
// assert(x != 0);
return power(*this, P - 2);
}
Z &operator*=(const Z &rhs) {
x = ll(x) * rhs.x % P;
return *this;
}
Z &operator+=(const Z &rhs) {
x = norm(x + rhs.x);
return *this;
}
Z &operator-=(const Z &rhs) {
x = norm(x - rhs.x);
return *this;
}
Z &operator/=(const Z &rhs) {
return *this *= rhs.inv();
}
friend Z operator*(const Z &lhs, const Z &rhs) {
Z res = lhs;
res *= rhs;
return res;
}
friend Z operator+(const Z &lhs, const Z &rhs) {
Z res = lhs;
res += rhs;
return res;
}
friend Z operator-(const Z &lhs, const Z &rhs) {
Z res = lhs;
res -= rhs;
return res;
}
friend Z operator/(const Z &lhs, const Z &rhs) {
Z res = lhs;
res /= rhs;
return res;
}
};
void solve(){
int n, K;
cin >> n >> K;
vector<int>arr(n);
for(auto &x : arr)cin >> x;
vector<int>s(K);
int mxid = 0;
for(int i = 0; i < K; ++i){
cin >> s[i];
if(s[i] > s[mxid])mxid = i;
}
vector<bool>use(n, false);
vector<vector<int>>ans(K);
auto divbox = [&](int id){
int len = s[id];
// cerr << id << " " << len << "\n";
vector<vector<int>>f(len, vector<int>(len, -1));
for(int i = 0; i < n; ++i)if(!use[i]){
for(int j = len - 2; ~j; --j){
for(int k = 0; k < len; ++k)if(~f[j][k]){
if(!~f[j + 1][(k + arr[i]) % len]){
f[j + 1][(k + arr[i]) % len] = i;
}
}
}
if(!~f[0][arr[i] % len])f[0][arr[i] % len] = i;
}
for(int i = len - 1, v = 0; ~i; --i){
int u = f[i][v];
use[u] = true;
ans[id].push_back(arr[u]);
(v -= arr[u]) %= len;
if(v < 0)v += len;
}
};
for(int i = 0; i < K; ++i)if(i != mxid)divbox(i);
int sum = 0;
for(int i = 0; i < n; ++i){
if(!use[i]){
ans[mxid].push_back(arr[i]);
(sum += arr[i]) %= s[mxid];
}
}
cout << s[mxid] - sum << "\n";
ans[mxid].push_back(s[mxid] - sum);
for(int i = 0; i < K; ++i){
sum = 0;
for(int j = 0 ; j < (int)ans[i].size(); ++j){
sum += ans[i][j];
cout << ans[i][j] << " \n"[j == (int)ans[i].size() - 1];
}
assert(sum % s[i] == 0);
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
solve();
return 0;
}
347. Top K Frequent Elements | 1503. Last Moment Before All Ants Fall Out of a Plank |
430. Flatten a Multilevel Doubly Linked List | 1290. Convert Binary Number in a Linked List to Integer |
1525. Number of Good Ways to Split a String | 72. Edit Distance |
563. Binary Tree Tilt | 1306. Jump Game III |
236. Lowest Common Ancestor of a Binary Tree | 790. Domino and Tromino Tiling |
878. Nth Magical Number | 2099. Find Subsequence of Length K With the Largest Sum |
1608A - Find Array | 416. Partition Equal Subset Sum |
1446. Consecutive Characters | 1618A - Polycarp and Sums of Subsequences |
1618B - Missing Bigram | 938. Range Sum of BST |
147. Insertion Sort List | 310. Minimum Height Trees |
2110. Number of Smooth Descent Periods of a Stock | 2109. Adding Spaces to a String |
2108. Find First Palindromic String in the Array | 394. Decode String |
902. Numbers At Most N Given Digit Set | 221. Maximal Square |
1200. Minimum Absolute Difference | 1619B - Squares and Cubes |
1619A - Square String | 1629B - GCD Arrays |